package com.itcam.daijia.driver.service.impl;

import com.itcam.daijia.common.constant.RedisConstant;
import com.itcam.daijia.common.execption.NGException;
import com.itcam.daijia.common.result.Result;
import com.itcam.daijia.common.result.ResultCodeEnum;
import com.itcam.daijia.dispatch.client.NewOrderFeignClient;
import com.itcam.daijia.driver.client.DriverInfoFeignClient;
import com.itcam.daijia.driver.service.DriverService;
import com.itcam.daijia.map.client.LocationFeignClient;
import com.itcam.daijia.model.form.driver.DriverFaceModelForm;
import com.itcam.daijia.model.form.driver.UpdateDriverAuthInfoForm;
import com.itcam.daijia.model.vo.driver.DriverAuthInfoVo;
import com.itcam.daijia.model.vo.driver.DriverLoginVo;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverServiceImpl implements DriverService {

    @Autowired
    private DriverInfoFeignClient driverInfoFeignClient;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LocationFeignClient locationFeignClient;

    @Autowired
    private NewOrderFeignClient newOrderFeignClient;


    /**
     * 小程序授权登录
     *
     * @param code 小程序登录时获取的 code
     * @return 返回登录结果，包含 token
     */
    @Override
    @SneakyThrows
    public String login(String code) {
        // 通过 Feign 客户端调用远程服务，获取 openId
        Long driverId = driverInfoFeignClient.login(code).getData();

        // 生成一个随机的 UUID 作为 token
        String token = UUID.randomUUID().toString().replaceAll("-", "");

        // 将 token 和 driverId 关联，并设置过期时间，存储在 Redis 中
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token, driverId.toString(), RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);

        // 返回生成的 token
        return token;
    }

    /**
     * 获取司机登录信息
     * @param driverId
     * @return
     */
    @Override
    public DriverLoginVo getDriverLoginInfo(Long driverId) {
        return driverInfoFeignClient.getDriverLoginInfo(driverId).getData();
    }

    /**
     * 获取司机认证信息
     * @param driverId
     * @return
     */
    @Override
    public DriverAuthInfoVo getDriverAuthInfo(Long driverId) {
        return driverInfoFeignClient.getDriverAuthInfo(driverId).getData();
    }

    /**
     * 更新司机认证信息
     * @param updateDriverAuthInfoForm
     * @return
     */
    @Override
    public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
        return driverInfoFeignClient.UpdateDriverAuthInfo(updateDriverAuthInfoForm).getData();
    }

    /**
     * 创建司机人脸模型
     * @param driverFaceModelForm
     * @return
     */
    @Override
    public Boolean creatDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
        return driverInfoFeignClient.creatDriverFaceModel(driverFaceModelForm).getData();
    }

    /**
     * 判断司机当日是否进行过人脸识别
     * @param driverId
     * @return
     */
    @Override
    public Boolean isFaceRecognition(Long driverId) {
        return driverInfoFeignClient.isFaceRecognition(driverId).getData();
    }

    /**
     * 验证司机人脸
     * @param driverFaceModelForm
     * @return
     */
    @Override
    public Boolean verifyDriverFace(DriverFaceModelForm driverFaceModelForm) {
        return driverInfoFeignClient.verifyDriverFace(driverFaceModelForm).getData();
    }

    /**
     * 开始接单服务
     * @param driverId
     * @return
     */
    @Override
    public Boolean startService(Long driverId) {
        // 判断认证状态
        DriverLoginVo driverLoginVo = driverInfoFeignClient.getDriverLoginInfo(driverId).getData();
        if (driverLoginVo.getAuthStatus().intValue() != 2) {
            throw new NGException(ResultCodeEnum.AUTH_ERROR);
        }
        // 判断当日是否人脸识别
        Boolean isFaceRecognition = driverInfoFeignClient.isFaceRecognition(driverId).getData();
        if (!isFaceRecognition) {
            throw new NGException(ResultCodeEnum.FACE_ERROR);
        }
        // 更新司机接单状态
        driverInfoFeignClient.updateServiceStatus(driverId, 1);
        // 删除司机位置信息
        locationFeignClient.removeDriverLocation(driverId);
        // 清空司机新订单队列
        newOrderFeignClient.clearNewOrderQueueData(driverId);
        return null;
    }

    /**
     * 停止接单服务
     * @param driverId
     * @return
     */
    @Override
    public Boolean stopService(Long driverId) {
        // 更新司机接单状态
        driverInfoFeignClient.updateServiceStatus(driverId, 0);
        // 删除司机位置信息
        locationFeignClient.removeDriverLocation(driverId);
        // 清空司机新订单队列
        newOrderFeignClient.clearNewOrderQueueData(driverId);
        return null;
    }

    

}
